<?php

include_once('ConnectionManager.php');

function selectAll() {
    $query = "SELECT * FROM USER ORDER BY USER_USERNAME ASC";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countByName($keyword) {
    $select = "SELECT USER_ID ";
    $from = "FROM USER ";
    $where = "WHERE USER_USERNAME LIKE '" . $keyword . "'";

    ConnectionManager::getInstance()->setQuery($select . $from . $where);
    ConnectionManager::getInstance()->query();
    return ConnectionManager::getInstance()->getNumRows();
}

function delete($id) {
    $sql = "DELETE FROM USER WHERE USER_ID = " . $id;
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function loadUserByID($userID) {
    $query = "SELECT * FROM USER WHERE USER_ID = $userID";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function loadUserByUsername($username) {
    $query = "SELECT * FROM USER WHERE USER_USERNAME = '$username'";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function loadUserByPassword($pass) {
    $query = "SELECT * FROM USER WHERE USER_PASSWORD = '$pass'";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function loadUser($username, $pass) {
    $query = "SELECT * FROM USER WHERE USER_USERNAME = '$username' AND USER_PASSWORD = '$pass'";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countTotalUser() {
    $query = "SELECT USER_ID FROM USER";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return ConnectionManager::getInstance()->getNumRows();
}

function update($userID, $username, $pass) {
    $sql = "UPDATE USER 
                SET USER_USERNAME = '$username', USER_PASSWORD = '$pass' 
                WHERE USER_ID = $userID";
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function updateUsername($userID, $pass) {
    $sql = "UPDATE USER 
                SET USER_PASSWORD = '$pass' 
                WHERE USER_ID = $userID";
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function updatePassword($userID, $pass) {
    $sql = "UPDATE USER 
                SET USER_PASSWORD = '$pass' 
                WHERE USER_ID = $userID";
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function insert($username, $pass) {
    $sql = "INSERT INTO USER (USER_USERNAME, USER_PASSWORD) 
                VALUES ('$username', '$pass')";
    ConnectionManager::getInstance()->setQuery($sql);
    return ConnectionManager::getInstance()->query();
}

function findBy($keyword, $start_position, $row_per_page) {
    $select = "SELECT USER_ID, USER_USERNAME, USER_PASSWORD FROM USER WHERE USER_USERNAME NOT LIKE (SELECT USER_USERNAME FROM USER WHERE USER_USERNAME LIKE 'superadmin') ";
    $where = "";


    if (isset($keyword)) {
        $where .= "AND USER_USERNAME LIKE '%$keyword%'";
    }

    $where .= " LIMIT " . $start_position . ", " . $row_per_page;
    ConnectionManager::getInstance()->setQuery($select . $where);
    return ConnectionManager::getInstance()->query();
}

?>